package com.servlet;

import com.servlet.bean.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

/**
 * @Title: SetSession
 * @Description:
 * @Auther: py
 * @Version: 1.0
 * @create 2023/12/25 13:33
 */
@WebServlet("/setsession")
public class SetSession extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1 获取session
        HttpSession session = req.getSession();
        //  为session添加过期时间
        String sessionId = session.getId();
        // 创建cookie
        Cookie cookie = new Cookie("JSESSIONID", session.getId());
        cookie.setMaxAge(60 * 10);
        // 发送浏览器s
        resp.addCookie(cookie);
        // 2 设置session
        User user = new User("admin", "123456");
        session.setAttribute("user", user);



        resp.getWriter().write("success");
    }
}
